package com.augurit.agmobile.agwater5.drainage.supervise.view

import android.app.Activity
import android.os.Bundle
import android.view.View
import androidx.core.graphics.toColorInt
import androidx.recyclerview.widget.GridLayoutManager
import com.augurit.agmobile.agwater5.drainage.common.constant.StringConstant
import com.augurit.agmobile.agwater5.drainage.common.ex.launchActivity
import com.augurit.agmobile.agwater5.drainage.common.ex.toColorSpan
import com.augurit.agmobile.agwater5.drainage.supervise.model.ShowType
import com.augurit.agmobile.agwater5.drainage.supervise.model.SuperviseType
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.arm.base.BaseActivity
import com.example.drainage.R
import com.example.drainage.databinding.ActivitySuperviseItemDetailBinding

/**
 * @author 创建人 ：yanghaozhang
 * @version 1.0
 * @package 包名 ：com.augurit.agmobile.agwater5.drainage.supervise.view
 * @createTime 创建时间 ：2022/1/4
 * @modifyBy 修改人 ：
 * @modifyTime 修改时间 ：
 * @modifyMemo 修改备注：
 */
class SuperviseItemDetailActivity : BaseActivity<ActivitySuperviseItemDetailBinding>() {

    private var facilityType: SuperviseType = SuperviseType.Reservoir

    private var showType: ShowType = ShowType.Editable

    private val adapter: ResultAdapter by lazy {
        ResultAdapter()
    }

    private var id: String? = null

    companion object {
        fun jump(activity: Activity, facilityType: SuperviseType, id: String, showType: ShowType) {
            activity.launchActivity<SuperviseItemDetailActivity> {
                putExtra(StringConstant.KEY_TYPE, facilityType)
                putExtra(StringConstant.KEY_ID, id)
                putExtra(StringConstant.KEY_EDITABLE, showType)
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?): ActivitySuperviseItemDetailBinding {
        return ActivitySuperviseItemDetailBinding.inflate(layoutInflater)
    }

    override fun initData(savedInstanceState: Bundle?) {
        initIntent()
        initView()
        initViewModel()
        initData()
    }

    //-----------------已定义方法---------------------------
    private fun initIntent() {
        if (intent.hasExtra(StringConstant.KEY_TYPE)) {
            facilityType = intent.getSerializableExtra(StringConstant.KEY_TYPE) as SuperviseType
        }
        if (intent.hasExtra(StringConstant.KEY_ID)) {
            id = intent.getStringExtra(StringConstant.KEY_ID)
        }
        if (intent.hasExtra(StringConstant.KEY_EDITABLE)) {
            showType = intent.getSerializableExtra(StringConstant.KEY_EDITABLE) as ShowType
        }
    }

    private fun initView() {
//        immersionStatusBar()
//        mBinding.apply {
//            titlebar.setTitle(facilityType.typeName)
//                .setTitleTextColor("#222222".toColor())
//                .setTitleTextSize(18f)
//                .setLeftImageIcon(R.mipmap.back_arrow, 14)
//                .setLeftImageTint("#222222".toColor())
//                .setLeftImageClick { finish() }
//                .setImmersion(true)
//                .setBackgroundResource(R.color.white)
//
//        }
        binding.apply {
            tvModuleName.text = "组织机构、制度建设及落实"
            tvSubModuleName.text = "重要小型水库未明确水库管理机构"
            tvResult.text = "*检查结果".toColorSpan(0..1, "#FFF7405A".toColorInt())

            when (showType) {
                ShowType.Editable -> {
                    adapter.setNewInstance(
                        mutableListOf(
                            ResultBean("正常"),
                            ResultBean("一般"),
                            ResultBean("较重"),
                            ResultBean("严重"),
                            ResultBean("特别严重"),
                            ResultBean("合理缺项"),
                        )
                    )
                    resultRecyclerView.adapter = adapter
                    resultRecyclerView.layoutManager = GridLayoutManager(this@SuperviseItemDetailActivity, 10).apply {
                        spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                            override fun getSpanSize(position: Int): Int {
                                return if (position == 0 || position == 1 || position == 4 || position == 5) 3 else 2
                            }
                        }
                    }

                }
                ShowType.ReadOnly -> {
                    resultRecyclerView.visibility = View.GONE
                    tvResultShow.visibility = View.VISIBLE
                    edDesc.hint = ""
                    edDesc.minHeight = 0
                    edDesc.minimumHeight = 0
                    edDesc.isFocusable = false
                    edDesc.isFocusableInTouchMode = false

                    tvResultShow.text = "正常"
                    edDesc.setText("一切正常")
                }
            }
        }
    }

    private fun initViewModel() {

    }

    private fun initData() {

    }

    data class ResultBean(val name: String, val bean: Any? = null)

    class ResultAdapter : BaseQuickAdapter<ResultBean, BaseViewHolder>(R.layout.item_supervise_item_detail) {
        var selectIndex = -1

        var onSelectListener: ((ResultBean) -> Unit)? = null

        override fun convert(holder: BaseViewHolder, item: ResultBean) {
            holder.setText(R.id.tvItemName, item.name)
            holder.itemView.isSelected = holder.absoluteAdapterPosition == selectIndex
            holder.itemView.setOnClickListener {
                if (holder.absoluteAdapterPosition != selectIndex) {
                    onSelectListener?.invoke(getItem(holder.absoluteAdapterPosition))

                    val oldIndex = selectIndex
                    selectIndex = holder.absoluteAdapterPosition
                    if (oldIndex in 0 until itemCount) {
                        notifyItemChanged(oldIndex)
                    }
                    notifyItemChanged(selectIndex)
                }
            }
        }

    }
}